// 一个用双列表简单做成的数据存储器
export default class XMap<K = string, V = any> {
    constructor(data?: [K, V]) {
      if (data) {
        this._keys = data.map((item) => item[0]);
        this._values = data.map((item) => item[1]);
      }
    }
    _keys: K[] = [];
    _values: V[] = [];
    get(key: K): V | undefined {
      const index = this._keys.indexOf(key);
      if (index === -1) {
        return undefined;
      }
      return this._values[index];
    }
    set(key: K, value: V): void {
      const index = this._keys.indexOf(key);
      if (index === -1) {
        this._keys.push(key);
        this._values.push(value);
      } else {
        this._values[index] = value;
      }
    }
    getByObj(key: K) {
      return {
        key: this.get(key),
        value: this.get(key),
      };
    }
    delete(key: K): void {
      const index = this._keys.indexOf(key);
      if (index === -1) {
        return;
      }
      this._keys.splice(index, 1);
      this._values.splice(index, 1);
    }
    has(key: K): boolean {
      return this._keys.includes(key);
    }
    clear(): void {
      this._keys = [];
      this._values = [];
    }
    size(): number {
      return this._keys.length;
    }
    keys(): K[] {
      return this._keys;
    }
    values(): V[] {
      return this._values;
    }
    entries(): [K, V][] {
      return this._keys.map((key, index) => [key, this._values[index]]);
    }
    forEach(
      cb: (key: K, value: V, index: number, map: XMap<K, V>) => void
    ): void {
      this._keys.forEach((key, index) => {
        cb(key, this._values[index], index, this);
      });
    }
  }
  